//
//  KRDiseaseVC.m
//  QuickCare
//
//  Created by 张凯锐 on 16/7/12.
//  Copyright © 2016年 lidabiao. All rights reserved.
//

#import "KRDiseaseVC.h"
#import "KRTreatmentVC.h"
#import "KRChoiceVC.h"
#import "KRComplicationVC.h"
#import "KRComplication.h"
#import "CJTTableViewController.h"

@interface KRDiseaseVC ()<treatmentVCDelegate,choiceVCDelegate,complicationVCDelegate>
//疾病名字
@property (weak, nonatomic) IBOutlet UILabel *diseaseNameLabel;

//确诊
@property (weak, nonatomic) IBOutlet UIButton *determineButton;
//疑似
@property (weak, nonatomic) IBOutlet UIButton *suspectedButton;
//接受
@property (weak, nonatomic) IBOutlet UIButton *acceptButton;
//未接受
@property (weak, nonatomic) IBOutlet UIButton *notAcceptButton;

//选择疾病类型
@property (weak, nonatomic) IBOutlet UIView *choiceView;
//类型
@property (weak, nonatomic) IBOutlet UILabel *choiceLable;

//选择并发症
@property (weak, nonatomic) IBOutlet UIView *complicationView;
//并发症
@property (weak, nonatomic) IBOutlet UILabel *complicationLabel;

//治疗方式
@property (weak, nonatomic) IBOutlet UIView *treatmentView;
//方式
@property (weak, nonatomic) IBOutlet UILabel *treatmentLabel;

//匹配诊所
@property (weak, nonatomic) IBOutlet UIView *clinicView;

//就诊申请
@property (weak, nonatomic) IBOutlet UIButton *applyButton;

//选择并发症id
@property (nonatomic,assign) NSInteger ci2_id;
//选择并发症数组
@property (nonatomic,strong) NSArray *complicationArr;
@end

@implementation KRDiseaseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"";
    
    //给疾病名字赋值名字
    self.diseaseNameLabel.text = [NSString stringWithFormat:@"疾病类型:  %@",self.diseaseName];
    //先把就诊申请禁止与用户交互和颜色设置为浅灰色
    self.applyButton.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0];
    self.applyButton.userInteractionEnabled = NO;
    //先把治疗方式和匹配诊所隐藏起来
    self.clinicView.hidden = YES;
    self.treatmentView.hidden = YES;
    
    //给治疗方式添加一个点击手势
    UITapGestureRecognizer *treatmentTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickTreatmentView:)];
    [self.treatmentView addGestureRecognizer:treatmentTapGesture];
    //给选择疾病类型添加一个点击手势
    UITapGestureRecognizer *choiceTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickChoiceView:)];
    [self.choiceView addGestureRecognizer:choiceTapGesture];
    //给选择并发症添加一个点击手势
    UITapGestureRecognizer *complicationTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickComplicationView:)];
    [self.complicationView addGestureRecognizer:complicationTapGesture];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [SVProgressHUD dismiss];
}

#pragma mark - 添加点击手势方法
//选择并发症方法
- (void)clickComplicationView:(UITapGestureRecognizer *)gesture {
    if ([self.choiceLable.text isEqualToString:@"请选择疾病类型"]) {
        [SVProgressHUD showErrorWithStatus:@"请选择疾病类型"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
    } else {
        KRComplicationVC *complicationVC = [[KRComplicationVC alloc] init];
        complicationVC.delegate = self;
        //给选择并发症传值请求参数
        complicationVC.ci1_id = self.index;
        complicationVC.ci2_id = self.ci2_id;
        [self.navigationController pushViewController:complicationVC animated:YES];
    }
}

//治疗方式触发方法
- (void)clickTreatmentView:(UITapGestureRecognizer *)gesture {
    KRTreatmentVC *treatmentVC = [[KRTreatmentVC alloc] init];
    treatmentVC.delegate = self;
    [self.navigationController pushViewController:treatmentVC animated:YES];
}

//选择疾病类型触发方法
- (void)clickChoiceView:(UITapGestureRecognizer *)gesture {
    KRChoiceVC *choiceVC = [[UIStoryboard storyboardWithName:@"Choice" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
    //传值
    choiceVC.index = self.index;
    choiceVC.delegate = self;
    [self.navigationController pushViewController:choiceVC animated:YES];
}

#pragma mark - 选择并发症代理方法
- (void)complicationVCDelegate:(KRComplicationVC *)delegate complicationName:(NSMutableAttributedString *)complicationName{
    //改变字体颜色
    self.complicationLabel.textColor = [UIColor blackColor];
    //将富文本赋给Label
    [self.complicationLabel setAttributedText:complicationName];
    //将Label的lineBreakMode变成NSLineBreakByTruncatingMiddle,这样省略号会变到中间
    self.complicationLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
}

#pragma mark - 选择疾病类型代理方法
- (void)choiceVCDelegate:(KRChoiceVC *)delegate choiceName:(NSString *)choiceName ci2_id:(NSInteger)ci2_id {
    //改变字体颜色和字
    self.choiceLable.textColor = [UIColor blackColor];
    self.choiceLable.text = choiceName;
    
    //传值选择并发症id
    self.ci2_id = ci2_id;
//    //如果请求不到数据就禁止与用户交互
//    [self requestData];
    self.complicationLabel.text = @"请选择并发症(可多选)";
    self.complicationLabel.textColor = [UIColor colorWithWhite:0.8 alpha:1.0];
}

#pragma mark - 治疗方式代理方法
- (void)treatmentVCDelegate:(KRTreatmentVC *)delegate treatmentName:(NSString *)treatmentName {
    if (treatmentName) {
        self.treatmentLabel.textColor = [UIColor blackColor];
        self.treatmentLabel.text = treatmentName;
        [UIView animateWithDuration:1.5 animations:^{
            self.clinicView.hidden = NO;
            self.applyButton.backgroundColor = [UIColor colorWithRed:54/255.f green:190/255.f blue:189/255.f alpha:0.6];
            self.applyButton.userInteractionEnabled = YES;
        }];
    }
}

#pragma mark - 点击确诊和疑似
- (IBAction)clickDetermine:(UIButton *)sender {
    if ([self.choiceLable.text isEqualToString:@"请选择疾病类型"]) {
        [SVProgressHUD showErrorWithStatus:@"请选择疾病类型"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
    } else {
        //点击的是确诊且为不选中才条件成立
        if (!(sender.selected) && [sender isEqual:self.determineButton]) {
            //把疑似设置为不选中,把确诊设置为选中
            self.suspectedButton.selected = NO;
            self.determineButton.selected = YES;
        }
        //点击的是疑似且为不选中才条件成立
        else if (!(sender.selected) && [sender isEqual:self.suspectedButton]) {
            //把确诊设置为不选中,把疑似设置为选中
            self.determineButton.selected = NO;
            self.suspectedButton.selected = YES;
        }
    }
}

#pragma mark - 点击接受和未接受
- (IBAction)clickAccept:(UIButton *)sender {
    if ([self.choiceLable.text isEqualToString:@"请选择疾病类型"]) {
        [SVProgressHUD showErrorWithStatus:@"请选择疾病类型"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
    } else {
        //点击的是接受且为不选中才条件成立
        if (!(sender.selected) && [sender isEqual:self.acceptButton]) {
            //把未接受设置为不选中,把接受设置为选中
            self.notAcceptButton.selected = NO;
            self.acceptButton.selected = YES;
            //把治疗方式设置为不隐藏
            self.treatmentView.hidden = NO;
            //把匹配诊所隐藏起来
            self.clinicView.hidden = YES;
            //把就诊申请设置为浅灰色和禁止与用户交互
            self.applyButton.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0];
            self.applyButton.userInteractionEnabled = NO;
        }
        //点击的是接受且为不选中才条件成立
        else if (!(sender.selected) && [sender isEqual:self.notAcceptButton]) {
            //把未接受设置为不选中,把接受设置为选中
            self.acceptButton.selected = NO;
            self.notAcceptButton.selected = YES;
            //把匹配诊所设置为不隐藏
            self.clinicView.hidden = NO;
            //把治疗方式隐藏起来
            self.treatmentView.hidden = YES;
            //把就诊申请设置为青色和可以与用户交互
            self.applyButton.backgroundColor = [UIColor colorWithRed:54/255.f green:190/255.f blue:189/255.f alpha:0.6];
            self.applyButton.userInteractionEnabled = YES;
            //如果用户点击了接受再点未接受,恢复到默认
            self.treatmentLabel.textColor = [UIColor colorWithWhite:0.8 alpha:1.0];
            self.treatmentLabel.text = @"请选择治疗方式";
        }
    }
}

//懒加载
- (NSArray *)complicationArr {
    if (_complicationArr == nil) {
        _complicationArr = [NSArray array];
    }
    return _complicationArr;
}

- (IBAction)AppleButton:(UIButton *)sender {
#pragma mark - 写到这里
    CJTTableViewController *VC = [[UIStoryboard storyboardWithName:@"CJTTableViewController" bundle:[NSBundle mainBundle]] instantiateInitialViewController];

    [self.navigationController pushViewController:VC animated:YES];
}


@end
